package br.com.logistica.database;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import br.com.logistica.base.BaseDAO;
import br.com.logistica.base.BaseException;
import br.com.logistica.databaseinterface.IGenericDAO;
import br.com.logistica.util.dto.ServiceDTO;
import br.com.logistica.vo.Cliente;
import br.com.logistica.vo.NotaFiscal;
import br.com.util.ConstantUtil;
import br.com.util.QueryUtil;

public class NotaFiscalDAO extends BaseDAO implements IGenericDAO, Serializable {

	private static Logger logger = Logger.getLogger(NotaFiscalDAO.class);

	@Override
	public ServiceDTO retrieve() throws BaseException {
		ServiceDTO result = new ServiceDTO();

		try {
			entityManager = FactoryJPA.getInstance(baseEnum);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			List<NotaFiscal> notaFiscals = entityManager.createQuery(
					" from  NotaFiscal tr").getResultList();

			result.set("notaFiscal", notaFiscals);

			if (notaFiscals.size() == 0) {
				result.set("sucesso", new Boolean(false));
			} else {
				result.set("sucesso", new Boolean(true));
			}
		} catch (Exception e) {
			logger.error(e);

			throw new BaseException(ConstantUtil.ERROR_SELECT_NF, e);
		}

		return (result);
	}

	@Override
	public ServiceDTO retrieve(ServiceDTO serviceDTO) throws BaseException {
		try {
			entityManager = FactoryJPA.getInstance(baseEnum);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		ServiceDTO result = new ServiceDTO();
		NotaFiscal notaFiscal = (NotaFiscal) serviceDTO.get("notaFiscal");

		try {
			notaFiscal = entityManager.find(NotaFiscal.class,
					notaFiscal.getIdNotaFiscal());

			result.set("notaFiscal", notaFiscal);

			if (notaFiscal == null) {
				result.set("sucesso", new Boolean(false));
			} else {
				result.set("sucesso", new Boolean(true));
			}
		} catch (Exception e) {
			logger.error(e);
		}

		return (result);
	}

	@Override
	public ServiceDTO restrictedRetrieve(ServiceDTO serviceDTO)
			throws BaseException {
		ServiceDTO result = new ServiceDTO();

		HashMap<String, Object> hashMap = (HashMap<String, Object>) serviceDTO
				.get("pesquisaRestritiva");

		result.set("pesquisaRestritiva",
				QueryUtil.fazerConsultaDinamica(hashMap, NotaFiscal.class));
		return result;
	}

	@Override
	public ServiceDTO create(ServiceDTO serviceDTO) throws BaseException {
		ServiceDTO result = new ServiceDTO();

		begin();

		try {

			NotaFiscal notaFiscal = (NotaFiscal) serviceDTO.get("notaFiscal");
			entityManager.persist(entityManager.merge(notaFiscal));
			commit();

		} catch (Exception e) {
			logger.error(e);

			rollback();
		}

		return (result);
	}

	@Override
	public ServiceDTO update(ServiceDTO serviceDTO) throws BaseException {
		ServiceDTO result = new ServiceDTO();

		begin();

		try {
			NotaFiscal notaFiscal = (NotaFiscal) serviceDTO.get("notaFiscal");

			entityManager.merge(notaFiscal);
			entityTransaction.commit();
		} catch (Exception e) {
			logger.error(e);

			entityTransaction.rollback();
		}

		return (result);
	}

	@Override
	public ServiceDTO delete(ServiceDTO serviceDTO) throws BaseException {
		ServiceDTO result = new ServiceDTO();

		begin();

		try {

			NotaFiscal notaFiscal = (NotaFiscal) serviceDTO.get("notaFiscal");
			entityManager.remove(entityManager.merge(notaFiscal));
			commit();

		} catch (Exception e) {
			logger.error(e);

			rollback();
		}

		return (result);
	}
}
